package com.hjm.project.terminal.library.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hjm.common.utils.yh.PoiUtil;
import com.hjm.project.terminal.library.common.SycnPosCommon;
import com.hjm.project.terminal.library.domain.TerminalLibrary;
import com.hjm.project.terminal.library.mapper.TerminalLibraryMapper;
import com.hjm.project.terminal.library.service.ITerminalLibraryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hjm.common.utils.text.Convert;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 终端库Service业务层处理
 * 
 * @author LOVE
 * @date 2020-11-26
 */
@Service
public class TerminalLibraryServiceImpl implements ITerminalLibraryService
{
    @Autowired
    private TerminalLibraryMapper terminalLibraryMapper;
    @Resource
    private SycnPosCommon sycnPosCommon;

    /**
     * 查询终端库
     * 
     * @param id 终端库ID
     * @return 终端库
     */
    @Override
    public TerminalLibrary selectTerminalLibraryById(Long id)
    {
        return terminalLibraryMapper.selectTerminalLibraryById(id);
    }

    /**
     * 查询终端库
     *
     * @param sn 终端号码
     * @return 终端库
     */
    @Override
    public TerminalLibrary selectTerminalLibraryBySn(String sn)
    {
        return terminalLibraryMapper.selectTerminalLibraryBySn(sn);
    }

    /**
     * 查询终端库列表
     * 
     * @param terminalLibrary 终端库
     * @return 终端库
     */
    @Override
    public List<TerminalLibrary> selectTerminalLibraryList(TerminalLibrary terminalLibrary)
    {
        return terminalLibraryMapper.selectTerminalLibraryList(terminalLibrary);
    }

    /**
     * 新增终端库
     * 
     * @param terminalLibrary 终端库
     * @return 结果
     */
    @Override
    public int insertTerminalLibrary(TerminalLibrary terminalLibrary)
    {
        return terminalLibraryMapper.insertTerminalLibrary(terminalLibrary);
    }

    /**
     * 修改终端库
     * 
     * @param terminalLibrary 终端库
     * @return 结果
     */
    @Override
    public int updateTerminalLibrary(TerminalLibrary terminalLibrary)
    {
        return terminalLibraryMapper.updateTerminalLibrary(terminalLibrary);
    }

    /**
     * 删除终端库对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTerminalLibraryByIds(String ids)
    {
        return terminalLibraryMapper.deleteTerminalLibraryByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除终端库信息
     * 
     * @param id 终端库ID
     * @return 结果
     */
    @Override
    public int deleteTerminalLibraryById(Long id)
    {
        return terminalLibraryMapper.deleteTerminalLibraryById(id);
    }

    @Override
    public Map<String, String> getKeyFile(String manuNum, Integer count)
    {
        Map<String,String> result=new HashMap<>(16);
        result.put("code","00");
        result.put("msg","操作成功");

        return result;
    }

    @Override
    public String getPosNo()
    {
        return terminalLibraryMapper.getPosNo().toString();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Map<String, String> addTerminal_library(File files, Map para)
    {
        Map<String,String> results=new HashMap<>(16);
        results.put("code","00");
        results.put("msg","操作成功");
        //读取文件
        Map responseMap = new HashMap();
        try{
            Map<String, Map<String, Object>> lines = PoiUtil.readExcelContent(files.getPath());
            if (lines.size()>10000){
                results.put("code","01");
                results.put("msg","终端数量超过一万台");
                return results;
            }
            int i=0;
            int j=0;
            List<String> snList = new ArrayList<>();
            for (Map ss : lines.values()) {
                String[] result = new String[6];
                Map<String, String> data = new HashMap();
                data.put("posNo", terminalLibraryMapper.getPosNo().toString());
                data.put("tmk", "");
                data.put("mitmk", "");
                data.put("ckv", "");
                data.put("sn", ss.get("key0") == null ? "" : ss.get("key0").toString());
                data.putAll(para);
                //判断参数是否为空
                result[4] = data.get("sn");
                if (StringUtils.isEmpty(data.get("sn"))) {
                    results.put("code","01");
                    results.put("msg","参数不能为空，请检查");
                    return results;
                } else {
                    //判断sn和终端号是否重复
                    int count = terminalLibraryMapper.havePosByPosNoOrSN(data);
                    if (count > 0) {
                        i++;
                    } else {
                        snList.add(result[4]);
                        j++;
                        results.put("code","00");
                        results.put("msg","导入成功数"+j+"重复数"+i);
                        //添加终端信息，终端不存在
                        terminalLibraryMapper.insertTerminal(data);
                    }
                }
            }
            sycnPosCommon.syncLsPos(snList,para);
        }catch (Exception e){
            e.printStackTrace();
            if (e.getMessage().indexOf("违反唯一约束条件 (MAKER_JK.POS_KEY_SN)") > -1) {
                results.put("code","01");
                results.put("msg","文件SN编号已存在数据库");
                return results;
            }

        }
        return results;
    }

}
